#coding:utf-8
__author__ = 'hl'
import  wx
import utils.submitData as submitData

class AddChannelUI(wx.Dialog):
    def __init__(self,parent,id):
        wx.Dialog.__init__(self,parent,id)
        self.SetTitle(u'输入渠道到信息')
        self.data = {'name':None,'encoded':None,'description':None}
        self.CreateUI()

    def CreateUI(self):
        #渠道名称
        channelNameLbl = wx.StaticText(self,-1,u'渠道名称：')
        self.channelNameTxt =wx.TextCtrl(self,-1,size=(200,20))

        #渠道编码
        channelEncodeLbl = wx.StaticText(self,-1,u'渠道编码：')
        self.channelEncodeTxt = wx.TextCtrl(self,-1,size=(200,20))

        #渠道描述
        channelDescriptLbl = wx.StaticText(self,-1,u'渠道描述：')
        self.channelDescriptTxt = wx.TextCtrl(self,-1,size=(200,100),style=wx.TE_MULTILINE)

        #操作
        submitData =wx.Button(self,-1,u'提交')
        cancel =wx.Button(self,wx.ID_CANCEL,u'取消')


        #布局
        mainSizer = wx.BoxSizer(wx.VERTICAL)

        #渠道名称
        subSizer = wx.FlexGridSizer(1,2,5,5)
        subSizer.Add(channelNameLbl,1,wx.EXPAND)
        subSizer.Add(self.channelNameTxt,1,wx.EXPAND|wx.LEFT,10)
        mainSizer.Add(subSizer,0,wx.ALL,10)

        #渠道编码
        subSizer = wx.FlexGridSizer(1,2,5,5)
        subSizer.Add(channelEncodeLbl,1,wx.EXPAND)
        subSizer.Add(self.channelEncodeTxt,1,wx.EXPAND|wx.LEFT,10)
        mainSizer.Add(subSizer,0,wx.ALL,10)

        #渠道编码
        subSizer = wx.FlexGridSizer(1,2,5,5)
        subSizer.Add(channelDescriptLbl,1,wx.EXPAND)
        subSizer.Add(self.channelDescriptTxt,1,wx.EXPAND|wx.LEFT,10)
        mainSizer.Add(subSizer,0,wx.ALL,10)

        #操作
        subSizer = wx.FlexGridSizer(1,2,5,5)
        subSizer.Add(submitData,1,wx.EXPAND|wx.LEFT,40)
        subSizer.Add(cancel,1,wx.EXPAND|wx.LEFT,20)
        mainSizer.Add(subSizer,0,wx.ALL,10)

        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self)

        #绑定事件
        submitData.Bind(wx.EVT_BUTTON,self.OnSubmit)
        self.channelNameTxt.Bind(wx.EVT_TEXT,self.OnInputName)
        self.channelEncodeTxt.Bind(wx.EVT_TEXT,self.OnInputEncode)
        self.channelDescriptTxt.Bind(wx.EVT_TEXT,self.OnInputDescription)

    def OnSubmit(self,evt):
        if not self.data['name']:
            print u'渠道名称为空'
            return
        if not self.data['encoded']:
            print u'渠道编码为空'
            return
        sendData = submitData.SubmitData()
        url = 'http://www.gongren8.com/index.php?app=admin&mod=Client&act=doAd_banben_type&id='
        rs = sendData.SubmitDataByPost(url,{'url':'http://www.gongren8.com/index.php?app=admin&mod=Client&act=platform_type','name':self.data['name'],
                                       'encoded':self.data['encoded'],
                                       'description':self.data['description']})
        if rs:
            print u'渠道添加成功'

            #向grid中增加一条数据
            parent = self.GetParent()
            parent.uiData.addDataForGridDatas([self.data['name'],self.data['encoded']])
            self.Close()





    def OnInputName(self,evt):
        self.data['name']=self.channelNameTxt.GetValue()

    def OnInputEncode(self,evt):
        self.data['encoded']=self.channelEncodeTxt.GetValue()

    def OnInputDescription(self,evt):
        self.data['description']=self.channelDescriptTxt.GetValue()







if __name__=='__main__':
    app = wx.PySimpleApp()
    add = AddChannelUI(None,-1)
    add.ShowModal()
    app.MainLoop()
